package com.yyplatform.admin.platform.role.controller;

import static com.yyplatform.admin.Constants.URL_PREFIX;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yyplatform.admin.infra.model.RestResponse;
import com.yyplatform.admin.platform.role.model.dto.SysMenuDTO;
import com.yyplatform.admin.platform.role.model.dto.SysMenuFormDTO;
import com.yyplatform.admin.platform.role.service.SysMenuService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Created on 2024-09-14
 */
@Slf4j
@Tag(name = "用户/菜单管理")
@AllArgsConstructor
@RestController
@RequestMapping(URL_PREFIX + "/sys/menu/")
public class SysMenuController {

    private SysMenuService sysMenuService;

    @GetMapping("query")
    @Operation(summary = "查询")
    @RequiresPermissions("sys:menu:query")
    public RestResponse<List<SysMenuDTO>> query() {

        List<SysMenuDTO> page = sysMenuService.listAllForTree();

        return RestResponse.ok(page);
    }


    @GetMapping("options")
    @Operation(summary = "查询for下拉框")
    public RestResponse<List<SysMenuDTO>> options(
            @RequestParam(required = false, defaultValue = "false") Boolean onlyRoot) {
        log.debug("get options ,params :onlyRoot={}", onlyRoot);

        return RestResponse.ok(sysMenuService.listForOptions(onlyRoot));
    }


    @GetMapping("get")
    @Operation(summary = "通过ID获取详情")
    @RequiresPermissions("sys:menu:get")
    public RestResponse<SysMenuDTO> get(Long id) {
        log.debug("get by id is {}", id);

        SysMenuDTO data = sysMenuService.get(id);
        return RestResponse.ok(data);
    }


    @PostMapping("save")
    @Operation(summary = "保存")
    @RequiresPermissions("sys:menu:save")
    public RestResponse<Void> save(@RequestBody SysMenuFormDTO sysMenuFormDTO) {

        log.debug("save dto is {}", sysMenuFormDTO);

        sysMenuService.save(sysMenuFormDTO);

        return RestResponse.ok(null);
    }


    @PostMapping("update")
    @Operation(summary = "修改")
    @RequiresPermissions("sys:menu:update")
    public RestResponse<Void> update(@RequestBody SysMenuFormDTO sysMenuFormDTO) {
        log.debug("update dto is {}", sysMenuFormDTO);

        sysMenuService.update(sysMenuFormDTO);
        return RestResponse.ok(null);
    }


    @PostMapping("delete")
    @Operation(summary = "删除")
    @RequiresPermissions("sys:menu:delete")
    public RestResponse<Void> delete(Long id) {
        log.debug("delete by id is {}", id);

        sysMenuService.delete(id);
        return RestResponse.ok(null);
    }


}
